package com.jichangxiu.dynamic.test.config;

import lombok.Data;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author Jcx
 * @date 2022/11/27 8:47
 */
@Configuration
@Data
public class ThreadPoolConfig {

    public static final String THREAD_POOL = "MyExecutor";

    /**
     * 核心线程
     */
    private int corePoolSize = 10;

    /**
     * 最大线程
     */
    private int maxPoolSize = 100;

    /**
     * 队列容量
     */
    private int queueCapacity = 100;

    /**
     * 保持时间
     */
    private int keepAliveSeconds = 60;

    /**
     * 名称前缀
     */
    private String preFix = "jcx_";

    @Bean("MyExecutor")
    public Executor myExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(corePoolSize);
        executor.setMaxPoolSize(maxPoolSize);
        executor.setQueueCapacity(queueCapacity);
        executor.setKeepAliveSeconds(keepAliveSeconds);
        executor.setThreadNamePrefix(preFix);
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
        executor.initialize();
        return executor;
    }

}
